package com.mgblog.web.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.metamodel.SetAttribute;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.mgblog.service.inter.ArticleSortServiceInter;
import com.mgblog.utils.Tools;
import com.mgblog.vo.ArticleSort;
import com.mgblog.vo.User;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * @author meigang 2014-11-26 16:28
 * 文章分类Action
 */
@Controller
public class ArticleSortAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArticleSort sort;
	@Resource
	private ArticleSortServiceInter articleSortService;
	
	public ArticleSort getSort() {
		return sort;
	}

	public void setSort(ArticleSort sort) {
		this.sort = sort;
	}
	/**
	 * 添加文章分类
	 * @return
	 */
	public String addArticleSort(){
		//因为突然struts2的表单封装出现问题，这里就用了request.getParameter()来取值了。
		String name = ServletActionContext.getRequest().getParameter("name");
		sort = new ArticleSort();
		sort.setName(name);
		sort.setArticleNum(0);
		sort.setCreateTime(new Date());
		sort.setUser((User) ActionContext.getContext().getSession().get(Tools.LOGIN_USER));
			
		//将文章分类查出来封装好
		if(articleSortService.addArticleSort(sort)){
			msg = "添加分类成功";
		}else{
			msg = "添加分类失败";
		}
		ActionContext.getContext().put("msg", msg);
		//将文章分类查出来封装好
		List<ArticleSort> asList = articleSortService.getAllArticleSorts("id asc");
		ActionContext.getContext().put("asList", asList);
				
		return "toWriteUI";
	}
	
}
